package com.sc.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sc.context.BaseContext;
import com.sc.domain.PayMeter;
import com.sc.dto.PayMeterPageDto;
import com.sc.mapper.PayMeterMapper;
import com.sc.mapper.csm.UserMapper;
import com.sc.result.PageResult;
import com.sc.service.PayMeterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class PayMeterServiceImpl implements PayMeterService {

    @Autowired
    PayMeterMapper payMeterMapper;
    @Autowired
    UserMapper userMapper;

    @Override
    public boolean save(PayMeter payMeter) {
        payMeter.setCreateTime(LocalDateTime.now());
        return payMeterMapper.save(payMeter);
    }

    @Override
    public boolean deleteById(Long id) {
        return payMeterMapper.deleteById(id);
    }

    @Override
    public boolean deleteByIds(List<Long> ids) {
        return payMeterMapper.deleteByIds(ids);
    }

    @Override
    public boolean update(PayMeter payMeter) {
        return payMeterMapper.update(payMeter);
    }

    @Override
    public PayMeter get(Integer id) {
        return payMeterMapper.get(id);
    }

    @Override
    public PageResult<PayMeter> page(PayMeterPageDto payMeterPageDto) {
        int start = (payMeterPageDto.getPage() - 1) * payMeterPageDto.getPageSize();
        int end = payMeterPageDto.getPage() * payMeterPageDto.getPageSize();

        List<PayMeter> list = payMeterMapper.list(payMeterPageDto);
        PageResult<PayMeter> page = new PageResult<>(list.size(), list.subList(start, Math.min(list.size(), end)));
        return page;
    }
}
